﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Asion.Dapper
{
    public static class DapperConfiguration
    {




        /// <summary>
        /// 默认code生成器
        /// </summary>
        public static IGenerateCode generate_code = new SimpleGenerateCode();

        /// <summary>
        /// connection factroy
        /// </summary>
        internal static IDbConnectionFactory connection_factory = new DbConnectionFactory();

        /// <summary>
        /// default connection
        /// </summary>
        internal static string default_connection = "";

        /// <summary>
        /// 是否属性初始化
        /// </summary>
        internal static bool is_properties_initialize = false;


        /// <summary>
        /// max  batch insert count 
        /// </summary>
        internal const int batch_insert_count = 500;

        /// <summary>
        /// max  batch update count 
        /// </summary>
        internal const int batch_update_count = 50;



        internal const int default_rows_page = 10;

        /// <summary>
        ///defalut entity key
        /// </summary>
        internal const string defalut_entity_key = "id";

        /// <summary>
        /// 默认扫描类名
        /// </summary>
        internal static string default_scan_name = "Asion";


        public static void ConfigurationConnectionFactory(IDbConnectionFactory connectionFactory)
        {
            connection_factory = connectionFactory;
            PropertiesInitialize();
        }



        /// <summary>
        /// Initalizer
        /// </summary>
        public static void ConfigurationConnection(string connectionString, DatabaseType dbType = DatabaseType.MySQL)
        {

            var configuration = new DbConfiguration()
            {
                Id = Guid.NewGuid().ToString(),
                ConnectionString = connectionString,
                DbType = dbType,
            };
            connection_factory.AddDbConfiguration(configuration);
            PropertiesInitialize();
        }


        /// <summary>
        /// Init db config by appsetting name
        /// </summary>
        public static void ConfigurationConnectionBySetting(string[] keys)
        {
            var list = new List<DbConfiguration>();
            foreach (var name in keys)
            {
                var configuration = DbConfiguration.GetDbConfigurationByXmlConfig(name);
                list.Add(configuration);
            }
            connection_factory.AddDbConfigurations(list);
            PropertiesInitialize();

        }

        public static void ConfigurationConnection(DbConfiguration dbConfiguration)
        {

            connection_factory.AddDbConfiguration(dbConfiguration);
            PropertiesInitialize();
        }


        public static void PropertiesInitialize()
        {
            if (!is_properties_initialize)
            {
                SqlMapperExtensions.PropertiesInitialize();
                is_properties_initialize = true;
            }
        }




        /// <summary>
        ///change default connection
        /// </summary>
        public static void ChangeDb(string name)
        {
            connection_factory.ChangeMasterConnection(name);
        }


         
        
        /// <summary>
        /// 设置扫描类名
        /// </summary>
        /// <param name="name"></param>
        public static void SetScanName(string name)
        { 
            default_scan_name=name;
        }



        #region 初始化状态相关暂时不要了
        /// <summary>
        /// init state
        /// </summary>
        private static InitializationStateEnum initialization_state = InitializationStateEnum.Start;

        public static bool IsCanInitialized()
        {
            if (InitializationStateEnum.Start == initialization_state)
            {
                return true;
            }
            return false;
        }


        public static bool IsInitialize()
        {
            if (initialization_state < InitializationStateEnum.Succeess)
            {
                return false;
            }
            return true;
        }

        private static void SetInitializeState(InitializationStateEnum state)
        {
            initialization_state = state;
        }

        #endregion


    }

    /// <summary>
    /// init state
    /// </summary>
    public enum InitializationStateEnum
    {
        /// <summary>
        /// do not init
        /// </summary>
        Start,
        /// <summary>
        /// init be ongoing
        /// </summary>
        Doing,
        /// <summary>
        /// init is success
        /// </summary>
        Succeess
    }
}
